﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ALiteTeam.PackageTracer.Service
{
    public class EmsHandler : IExQueryHandler
    {
        private static readonly string TARGET_IMGURI = "http://www.ems.com.cn/ems/rand";
        private static readonly string TRAGET_URI = "http://www.ems.com.cn/ems/order/singleQuery_t";

        public QueryResult QuerySummary(string orderNo)
        {
            QueryResult result = null;

            string content = GetContent(orderNo);
            result = Filter.GetSummary(content);

            return result;
        }

        public QueryResult QueryDetails(string orderNo)
        {
            QueryResult result = null;

            string content = GetContent(orderNo);
            result = Filter.GetDetails(content);

            return result;
        }

        private string GetContent(string orderNo)
        {
            string content = string.Empty;

            try
            {
                CookieContainer cookieContainer = new CookieContainer();

                string code = GetCode(cookieContainer);
                content = GetContent(code, orderNo, cookieContainer);
            }
            catch
            {
            }

            return content;
        }

        private string GetCode(CookieContainer cookieContainer)
        {
            string code = "?";

            try
            {
                do
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(TARGET_IMGURI);
                    request.CookieContainer = cookieContainer;
                    request.Accept = "image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5";

                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream streamImg = response.GetResponseStream();
                    code = this.Identitier.Identity(streamImg);

                }
                while (code.Contains("_") == true);
            }
            catch
            {
            }

            return code;
        }

        private string GetContent(string code, string postID, CookieContainer cookieContainer)
        {
            string result = string.Empty;

            try
            {
                string formString = string.Format("mailNum={0}&checkCode={1}", postID, code);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(TRAGET_URI);

                request.Accept = "text/html, application/xhtml+xml, */*";
                request.Method = "post";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = formString.Length;
                request.CookieContainer = cookieContainer;

                Stream streamRequest = request.GetRequestStream();
                byte[] buff = Encoding.UTF8.GetBytes(formString);

                streamRequest.Write(buff, 0, buff.Length);
                streamRequest.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();

                StreamReader reader = new StreamReader(stream);
                result = reader.ReadToEnd();
            }
            catch
            {
            }

            return result;
        }

        public Identify Identitier { get; set; }
        public BaseFilter Filter { get; set; }
    }
}
